package carltechnology.com.mahjonggame.views.impl;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import carltechnology.com.mahjonggame.R;
import carltechnology.com.mahjonggame.base.MyApplication;
import carltechnology.com.mahjonggame.presenter.IMahjongPerGameInputPresenter;
import carltechnology.com.mahjonggame.presenter.impl.MahjongPerGameInputPresenter;
import carltechnology.com.mahjonggame.utils.views.TextEditView;
import carltechnology.com.mahjonggame.views.IMahjongPerGameInputView;

/**
 * Created by WZTCM on 2016/6/16.
 */
public class MahjongPerGameInputFragment extends Fragment implements IMahjongPerGameInputView{
    public enum PERGAME_MSG{
        PRICE_NOTHING("请正确输入每人的张数"),
        COST_NOTHING("请输入台费"),
        BALANCE_UNRIHGT("平衡因子不正确，请检查输入"),
        UPDATE_BALANCE("获得新平衡因子");
        private String msg;
        PERGAME_MSG(String msg) {
            this.msg = msg;
        }
        @Override
        public String toString() {
            return msg;
        }
    }

    @InjectView(R.id.mahjonggame_account_dialog_person1)
    TextEditView person1View;
    @InjectView(R.id.mahjonggame_account_dialog_person2)
    TextEditView person2View;
    @InjectView(R.id.mahjonggame_account_dialog_person3)
    TextEditView person3View;
    @InjectView(R.id.mahjonggame_account_dialog_person4)
    TextEditView person4View;
    @InjectView(R.id.mahjonggame_account_dialog_cost)
    TextEditView costView;
    @InjectView(R.id.mahjonggame_account_dialog_balance)
    TextView balanceView;

    private IMahjongPerGameInputPresenter myPresenter;

    @Override
    public void sendMsgToFragment(PERGAME_MSG msg) {
        Message message = new Message();
        message.obj = msg;
        myHandler.sendMessage(message);
    }

    @Override
    public void sendBalanceFactorToFragment(int balance) {
        Message message = new Message();
        message.obj = PERGAME_MSG.UPDATE_BALANCE;
        message.arg1 = balance;
        myHandler.sendMessage(message);
    }

    public void sendBalanceChanged(){

    }
    @Override
    public String getPerson1Content() {
        return person1View.getEditTextContent();
    }

    @Override
    public String getPerson2Content() {
        return person2View.getEditTextContent();
    }

    @Override
    public String getPerson3Content() {
        return person3View.getEditTextContent();
    }

    @Override
    public String getPerson4Content() {
        return person4View.getEditTextContent();
    }

    @Override
    public String getCostContent() {
        return costView.getEditTextContent();
    }

//    @Override
//    public TextView getBalanceView() {
//        return balanceView;
//    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mahjonggame_account_dialog,container,false);
        ButterKnife.inject(this,view);
        myPresenter = MahjongPerGameInputPresenter.getInstance(this);
        myPresenter.addTextWatcher(person1View.getEditText());
        myPresenter.addTextWatcher(person2View.getEditText());
        myPresenter.addTextWatcher(person3View.getEditText());
        myPresenter.addTextWatcher(person4View.getEditText());
        myPresenter.addTextWatcher(costView.getEditText());
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        String [] names = MyApplication.getDataPool().getMahjongGameMode().getName();
        person1View.setEditTextTag(names[0]);
        person2View.setEditTextTag(names[1]);
        person3View.setEditTextTag(names[2]);
        person4View.setEditTextTag(names[3]);
    }

    private Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            PERGAME_MSG myMsg = (PERGAME_MSG) msg.obj;
            Snackbar.make(getView(),myMsg.toString(),Snackbar.LENGTH_SHORT)
                    .setAction("Undo", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    })
                    .show();
            if (myMsg == PERGAME_MSG.UPDATE_BALANCE){
               // System.out.println(msg.arg1);
                balanceView.setText(String.valueOf(msg.arg1));
            }

        }
    };



}
